﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
    public partial class 诊疗退费 : Form
    {
        public 诊疗退费()
        {
            InitializeComponent();
        }

        

        private void button_tc_Click(object sender, EventArgs e)
        {
            this.Hide();
            
        }

        private void button_qp_Click(object sender, EventArgs e)
        {
            tb_ch.Text = "";
            tb_nl.Text = "";
            tb_ye.Text = "";
            tb_xb.Text = "";
            tb_xm.Text = "";
            tb_jzys.Text = "";
            tb_ZYNo.Text = "";
            DataTable dt = (DataTable)dataGridView1.DataSource;
            dt.Rows.Clear();
            dataGridView1.DataSource = dt;

        }

        private void tb_ZYNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string ZYNo = tb_ZYNo.Text.Trim();


                if (ZYNo == "")
                {
                    MessageBox.Show("请输入要查询的内容！");
                    tb_ZYNo.Focus();

                    return;
                }
                string Hos = "select * from InHospital,Patient,Bed where InHospital.HosNo='" + ZYNo + "'and InHospital.HosNo=Patient.HosNo and InHospital.BedNo=Bed.BedNo";
                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {

                    try
                    {
                        conn.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    SqlCommand cmd = new SqlCommand(Hos, conn);


                    if (ZYNo != "")
                    {
                        SqlDataReader dr = cmd.ExecuteReader();

                        if (dr.HasRows)
                        {
                            dr.Read();

                            tb_ch.Text = dr["BedNo"].ToString();
                            int nf = Convert.ToInt32(dr["BronYear"].ToString());
                            DateTime time = System.DateTime.Now;
                            int year = time.Year;
                            int nl = year - nf;
                            tb_nl.Text = nl.ToString();
                            tb_ye.Text = dr["HosBalance"].ToString();
                            tb_xb.Text = dr["PatSex"].ToString();
                            tb_xm.Text = dr["PatName"].ToString();
                            tb_jzys.Text = dr["HosDepartment"].ToString();
                            string dno = dr["DocNo"].ToString();
                            dr.Close();
                            string DNo = "select DocName from Doctor where  DocNo='" + dno + "' ";                           
                            SqlCommand D = new SqlCommand(DNo, conn);                          
                            string DD = Convert.ToString(D.ExecuteScalar());                            
                            tb_jzys.Text = DD;

                        }
                        dr.Close();

                    }
                    string WarContent = "select WarContent,MedDanwei,Medprice,MedModel,MedAmount,Medscalar ,AmountPaid from ConsumptionSum where HosNo='" + ZYNo + "'and RefundCondition='申请退款'";
                 
                   
                  SqlCommand comContent = new SqlCommand(WarContent, conn);
                    SqlDataAdapter da = new SqlDataAdapter();
                    DataSet ds = new DataSet();
                    da = new SqlDataAdapter(WarContent, conn);
                    ds = new DataSet();
                    da.Fill(ds);
                    
                    dataGridView1.DataSource = ds.Tables[0];

                    int size = dataGridView1.RowCount;

                    double sum = 0;

                    for (int i = 0; i < size; i++)
                    {
                        sum += Convert.ToDouble(dataGridView1.Rows[i].Cells[7].Value);
                        lb_hj.Text =  sum.ToString();
                    }
                        
                    }
                }
            }

        private void button_tf_Click(object sender, EventArgs e)
        {
            string HosBalance = "select HosBalance from InHospital where HosNo='" + tb_ZYNo.Text + "'";
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                int Rcount = dataGridView1.Rows.Count - 1;
                conn.Open();
                SqlCommand com = new SqlCommand(HosBalance, conn);
                double Balance = Convert.ToDouble(com.ExecuteScalar());
                double m = Convert.ToDouble(lb_hj .Text );
                float NewBalance = (float)(Balance + m);

                string HosTotal = "select HosTotal from InHospital where HosNo='" + tb_ZYNo.Text.Trim() + "'";
                SqlCommand cm = new SqlCommand(HosTotal, conn);
                double Total = Convert.ToDouble(cm.ExecuteScalar());
                float NewTotal = (float)(Total - m);

                string inh = string.Format("update InHospital set HosBalance='" + NewBalance + "',HosTotal= '" + NewTotal + "' where InHospital.HosNo='" + tb_ZYNo.Text.Trim() + "'");
                string war = string.Format("update Warn set WarnCondition='申退成功' where  HosNo='" + tb_ZYNo.Text + "'and WarnCondition='申退'");
                string cps = string.Format("update ConsumptionSum set RefundCondition='已退款'where  HosNo='" + tb_ZYNo.Text + "' and RefundCondition='申请退款'");

                SqlCommand INH = new SqlCommand(inh, conn);
                SqlCommand WAR = new SqlCommand(war, conn);
                int z = Convert.ToInt32(INH.ExecuteNonQuery());
                int y = Convert.ToInt32(WAR.ExecuteNonQuery());
                SqlCommand CPS = new SqlCommand(cps , conn);
                int i = Convert.ToInt32(CPS.ExecuteNonQuery());
                if (i == Rcount && z == 1 && y == Rcount)
                    MessageBox.Show("退费成功！");
                else
                    MessageBox.Show("退费失败！");
            }

            }
           
            
        }
        
    }

